package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysExamine;
import com.ruoyi.system.mapper.SysExamineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysDomainNameMapper;
import com.ruoyi.system.domain.SysDomainName;
import com.ruoyi.system.service.ISysDomainNameService;

import javax.annotation.Resource;

/**
 * 域名Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-29
 */
@Service
public class SysDomainNameServiceImpl extends ServiceImpl<SysDomainNameMapper, SysDomainName> implements ISysDomainNameService
{

    @Resource
    private SysExamineMapper sysExamineMapper;

    /**
     * 查询域名列表
     *
     *
     * @param sysDomainName 域名
     * @return 域名
     */
    @Override
    public List<SysDomainName> selectSysDomainNameList(SysDomainName sysDomainName)
    {
        return baseMapper.selectSysDomainNameList(sysDomainName);
    }

    @Override
    public String importUser(List<SysDomainName> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入域名数据不能为空！");
        }
        // 获取查询检测配置数据
        SysExamine sysExamine = sysExamineMapper.selectOne((new LambdaQueryWrapper<SysExamine>().eq(SysExamine::getDeleted, 0)));
        // 判断是否为空
        if(Objects.isNull(sysExamine)){
            throw new ServiceException("未获取到检测配置数据");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SysDomainName user : userList)
        {
            user.setThreshold(sysExamine.getThreshold());
            baseMapper.insert(user);
            successNum++;
            successMsg.append("<br/>" + successNum + "、账号 " + user.getDomainName() + " 导入成功");
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
